// Validation Checked
package edu.cmu.fithub.formbeans;

import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

import org.mybeans.form.FormBean;

public class RegisterForm extends FormBean {

	private String username;
	private String password;
	private String passwordConf;
	private String age;
	private String firstName;
	private String lastName;
	private String timeZone;
	private String email;
	private String weight;
	private String action;

	public String getUsername() {
		return username;
	}

	public String getPasswordConf() {
		return passwordConf;
	}

	public void setPasswordConf(String passwordConf) {
		this.passwordConf = passwordConf;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(String timeZone) {
		this.timeZone = timeZone;
	}

	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public List<String> getValidationErrors() {
		List<String> errors = new ArrayList<String>();

		if (username == null || username.length() == 0) {
			errors.add("UserName is required");
		} else if (username.matches(".*[<>].*")) {
			errors.add("Username may not contain angle brackets");
			username = username.replaceAll(">", "&gt;");
			username = username.replaceAll("<", "&lt;");
			username = username.replaceAll(" ", "&nbsp;");
		}
		if (email == null || email.length() == 0) {
			
			errors.add("Email is required");
		} else {
			if (email.matches(".*[<>\"].*")) {
				errors.add("Email may not contain angle brackets or quotes");
			}
			if (!email.contains("@")) {
				errors.add("Not a valid email");
			}
		}

		if (firstName == null || firstName.length() == 0) {
			errors.add("First Name is required");
		} else if (firstName.matches(".*[<>\"].*")) {
			errors.add("First Name may not contain angle brackets or quotes");
		}
		if (lastName == null || lastName.length() == 0) {
			errors.add("Last Name is required");
		} else if (lastName.matches(".*[<>\"].*")) {
			errors.add("Last Name may not contain angle brackets or quotes");
		}
		if (age == null || age.length() == 0) {
			errors.add("Age is required");
		} else if (!age.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
			errors.add("Age must be a valid number");
		}
		if (weight == null || weight.length() == 0) {
			errors.add("Weight is required");
		} else if (!weight.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
			errors.add("Weight must be a valid number");
		}
		if (password == null || password.length() == 0) {
			errors.add("Password is required");
		} else if (password.matches(".*[<>].*")) {
			errors.add("Password may not contain angle brackets");
			password = "";
			passwordConf = "";
		}
		if (passwordConf == null || passwordConf.length() == 0) {
			errors.add("Confirmation Password is required");
		}
		if (!password.equals(passwordConf)) {
			errors.add("Passwords do not match");
			password = "";
			passwordConf = "";
		}
		if (timeZone == null || timeZone.length() == 0) {
			errors.add("Time Zone is required");
		} else {
			TimeZone tZone = TimeZone.getTimeZone(timeZone.trim());
			if ((!timeZone.equals("GMT") && (tZone.getID().equals("GMT")))) {
				errors.add("Time Zone is invalid");
			}
		}

		return errors;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}
}
